package com.zoffy.wmsbackend.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zoffy.wmsbackend.common.ErrorCode;
import com.zoffy.wmsbackend.common.RequestParam;
import com.zoffy.wmsbackend.exception.BusinessException;
import com.zoffy.wmsbackend.mapper.WarehouseMapper;
import com.zoffy.wmsbackend.model.dto.stockin.StockInOrderDto;
import com.zoffy.wmsbackend.model.dto.stockin.StockInDTO;
import com.zoffy.wmsbackend.model.dto.stockin.StockInDetailDTO;
import com.zoffy.wmsbackend.model.entity.*;
import com.zoffy.wmsbackend.model.excel.StockInImportListener;
import com.zoffy.wmsbackend.model.vo.stock.stockIn.StockInDetailVo;
import com.zoffy.wmsbackend.model.vo.stock.stockIn.StockInOrderDetailsVo;
import com.zoffy.wmsbackend.model.vo.stock.stockIn.StockInOrderVo;
import com.zoffy.wmsbackend.service.GoodsService;
import com.zoffy.wmsbackend.service.StockInOrderDetailService;
import com.zoffy.wmsbackend.service.StockInOrderService;
import com.zoffy.wmsbackend.mapper.StockInOrderMapper;
import com.zoffy.wmsbackend.utils.StockUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
* @author 86170
* @description 针对表【stock_in_order(入存表)】的数据库操作Service实现
* @createDate 2025-01-02 01:34:40
*/
@Service
public class StockInOrderServiceImpl extends ServiceImpl<StockInOrderMapper, StockInOrder>
    implements StockInOrderService {
    @Resource
    private GoodsService goodsService;
    @Resource
    private WarehouseMapper warehouseMapper;
    @Resource
    private StockInOrderDetailService stockInOrderDetailService;
    @Resource
    private StockUtil stockUtil;
    @Autowired
    private StockInOrderMapper stockInOrderMapper;

    @Override
    @Transactional
    public Boolean stockIn(StockInDTO stockInDTO) {
        Map<String, Goods> goodsMap = goodsService.list().stream().collect(Collectors.toMap(Goods::getGoodsCode, k -> k));
        List<StockInOrderDetail> stockInOrderDetails= new ArrayList<>();
        String stockInNo = stockInDTO.getStockInNo();
        List<StockInDetailDTO> stockInDetails = stockInDTO.getStockInDetails();
        String warehouseNo = stockInDTO.getWarehouseNo();
        LambdaQueryWrapper<Warehouse> queryWrapper= Wrappers.lambdaQuery();
        queryWrapper.eq(Warehouse::getWarehouseCode,warehouseNo);
        Warehouse warehouse = warehouseMapper.selectOne(queryWrapper);
        if(warehouse==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"仓库不存在");
        }
        StockInOrder stockInOrder = new StockInOrder();
        stockInOrder.setStockInNo(stockInNo);
        stockInOrder.setWarehouseId(warehouse.getId());
        stockInOrder.setStatus(0);
        this.save(stockInOrder);
        stockInDetails.forEach(stockInDetail -> {
            String goodsNo = stockInDetail.getGoodsNo();
            Integer num = stockInDetail.getNum();
            Goods goods = goodsMap.get(goodsNo);
            StockInOrderDetail stockInOrderDetail = new StockInOrderDetail();
            stockInOrderDetail.setStockInOrderId(stockInOrder.getId());
            stockInOrderDetail.setStockInDetailNo(stockInDetail.getStockInDetailNo());
            stockInOrderDetail.setWarehouseId(warehouse.getId());
            stockInOrderDetail.setGoodsId(goods.getId());
            stockInOrderDetail.setQuantity(num);
            stockInOrderDetail.setStatus(0);
            stockInOrderDetails.add(stockInOrderDetail);
        });
        stockInOrderDetailService.saveBatch(stockInOrderDetails);
        return true;
    }
    @Override
    public Boolean importStockIn(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(),null,new StockInImportListener()).sheet().doRead();
        return true;
    }

    @Override
    public Page<StockInOrderVo> listStockInOrderVoByPage(RequestParam<StockInOrderDto> requestParam) {
        Long pageSize = requestParam.getPageSize();
        Integer pageNum = requestParam.getPageNum();
        StockInOrderDto dto = requestParam.getParams();
        Page<StockInOrderVo> page=new Page<>(pageNum,pageSize);
        Page<StockInOrderVo> res = stockInOrderMapper.listStockInOrderVoByPage(dto, page);
        return res;
    }

    @Override
    public Boolean approval(Long stockInId) {
        StockInOrder stockInOrder = this.getById(stockInId);
        stockInOrder.setStatus(90);
        this.updateById(stockInOrder);
        LambdaQueryWrapper<StockInOrderDetail> queryWrapper=Wrappers.lambdaQuery();
        queryWrapper.eq(StockInOrderDetail::getStockInOrderId,stockInId);
        List<StockInOrderDetail> stockInOrderDetails = stockInOrderDetailService.list(queryWrapper);
        stockUtil.addStock(stockInOrderDetails);
        return true;
    }

    @Override
    public StockInOrderDetailsVo getDetailsById(Long stockInId) {
        StockInOrderVo stockInVo = stockInOrderMapper.getStockInVoById(stockInId);
        StockInOrderDetailsVo stockInOrderDetailsVo = new StockInOrderDetailsVo();
        stockInOrderDetailsVo.setStockInId(stockInId);
        stockInOrderDetailsVo.setWarehouseCode(stockInVo.getWarehouseCode());
        stockInOrderDetailsVo.setWarehouseName(stockInVo.getWarehouseName());
        stockInOrderDetailsVo.setWarehouseId(stockInVo.getWarehouseId());
        stockInOrderDetailsVo.setStockInNo(stockInVo.getStockInNo());
        List<StockInDetailVo> details = stockInOrderMapper.getDetailsById(stockInId);
        stockInOrderDetailsVo.setDetails(details);
        return stockInOrderDetailsVo;
    }
}




